package cn.regexp.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.regexp.domain.ShopCart;
import cn.regexp.domain.ShopGoods;
import cn.regexp.mapper.CartMapper;
import cn.regexp.service.ICartService;
import cn.regexp.service.IGoodsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, ShopCart> implements ICartService {
    @Autowired
    private IGoodsService goodsService;

    @Override
    public Boolean addGoods(Long goodsId, Long userId) {
        ShopGoods goods = goodsService.getById(goodsId);
        if (ObjectUtil.isEmpty(goods)) {
            return false;
        }
        ShopCart shopCart = getShopCart(goodsId, userId);
        if (ObjectUtil.isEmpty(shopCart)) {
            shopCart = new ShopCart();
            shopCart.setGoodsId(goodsId);
            shopCart.setGoodsPrice(goods.getGoodsPrice());
            shopCart.setGoodsImageUrl(goods.getCoverUrl());
            shopCart.setGoodsName(goods.getGoodsName());
            shopCart.setNumber(1);
            shopCart.setUserId(userId);
            shopCart.setWindowId(goods.getWindowId());
            shopCart.setWindowName(goods.getWindowName());
            shopCart.setIsCheck(1);
            return super.save(shopCart);
        } else {
            shopCart.setNumber(shopCart.getNumber() + 1);
            return super.updateById(shopCart);
        }
    }

    @Override
    public Boolean subGoods(Long goodsId, Long userId) {
        ShopCart shopCart = getShopCart(goodsId, userId);
        shopCart.setNumber(shopCart.getNumber() - 1);
        return super.updateById(shopCart);
    }

    private ShopCart getShopCart(Long goodsId, Long userId) {
        QueryWrapper<ShopCart> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id", goodsId)
                .eq("user_id", userId)
                .eq("is_pay", ShopCart.NO);
        return super.getOne(wrapper);
    }

    @Override
    public Boolean delGoods(Long goodsId, Long userId) {
        QueryWrapper<ShopCart> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id", goodsId)
                .eq("user_id", userId);
        return super.remove(wrapper);
    }

    @Override
    public Boolean changeGoodsChecked(Long goodsId, Long userId) {
        ShopCart shopCart = getShopCart(goodsId, userId);
        shopCart.setIsCheck(shopCart.getIsCheck() == 0 ? 1 : 0);
        return super.updateById(shopCart);
    }

    @Override
    public Boolean changeGoodsAllChecked(Long windowId, int isCheck, Long userId) {
        UpdateWrapper<ShopCart> wrapper = new UpdateWrapper<>();
        wrapper.set("is_check", isCheck)
                .eq("window_id", windowId).eq("user_id", userId);
        return super.update(wrapper);
    }
}
